#include"Comm.hpp"
//using namespace std;

int main()
{

    //1、获取key
    key_t key = GetKey();
    
    //2、创建共享内存
    int shmid = CreateShm(key);

    //3、挂载共享内存
    char* s = (char*)shmat(shmid,nullptr,0);
    if(s == (char*)-1)
    {
        std::cerr << "errno: " << errno << ", errstring: " << strerror(errno) << std::endl;
        exit(1);
    }
    std::cout << "Service shmat success-------" << std::endl;
    
    //4、创建管道，实现同步机制
    bool ret = MakeFifo();
    if(!ret)
    {
        exit(1);
    }
    int fd = open(fifoname.c_str(), O_RDONLY);
    
    //5、接收客户端信息
    while(true)
    {
        int code = 0;
        int n = read(fd,&code,sizeof(code)); //等待通知
        std::cout << "共享内存中："<< s << std::endl;
        if(n == 0)
        {
            break;
        }
    }
    //6、服务端关闭，释放共享内存
    shmctl(shmid,IPC_RMID,nullptr);
    std::cout << "Service shmrm success-------" << std::endl;
    //7、删除管道文件
    unlink(fifoname.c_str());
    std::cout << "Service fifo unlink success-------" << std::endl;
    return 0;
}
